// The sample model.  You should build a file
// very similar to this for when you make your model.
#include "modelerview.h"
#include "modelerapp.h"
#include "modelerdraw.h"
#include <FL/gl.h>

#include "modelerglobals.h"

// To make a SampleModel, we inherit off of ModelerView
class SampleModel : public ModelerView 
{
public:
    SampleModel(int x, int y, int w, int h, char *label) 
        : ModelerView(x,y,w,h,label) { }

    virtual void draw();
};

// We need to make a creator function, mostly because of
// nasty API stuff that we'd rather stay away from.
ModelerView* createSampleModel(int x, int y, int w, int h, char *label)
{ 
    return new SampleModel(x,y,w,h,label); 
}

void drawSea()
{
	setAmbientColor(.1f,.1f,.1f);
	setDiffuseColor(COLOR_BLUE);
	glPushMatrix();
	glTranslated(-5,0,-5);
	drawBox(10,0.01f,10);
	glPopMatrix();
}

void drawFloor()
{
	setAmbientColor(.1f,.1f,.1f);
	setDiffuseColor(COLOR_RED);
	glPushMatrix();
	glTranslated(-5,0,-5);
	drawBox(10,0.01f,10);
	glPopMatrix();
}

void drawLeg()
{
	glPushMatrix();
		setAmbientColor(.1f,.1f,.1f);
		setDiffuseColor(COLOR_RED);
		drawCylinder(1, 0.2, 0.2);
	glPopMatrix();
}

void drawArm()
{
	glPushMatrix();
		setAmbientColor(.1f,.1f,.1f);
		setDiffuseColor(COLOR_RED);
		drawCylinder(2, 0.2, 0.2);
	glPopMatrix();
}

void drawHead()
{
	glPushMatrix();
		setAmbientColor(.1f,.1f,.1f);
		setDiffuseColor(COLOR_BLUE);
		drawSphere(1);
	glPopMatrix();
}

void drawBody()
{
	glPushMatrix();
		glRotated(270, 1, 0, 0);
		glTranslated(0, 0, 1);
		drawCylinder(3, 1, 1);

		glPushMatrix();
			glRotated(180, 1, 0, 0);
			glTranslated(0.5, 0, 0);
			drawLeg();
		glPopMatrix();

		glPushMatrix();
			glRotated(180, 1, 0, 0);
			glRotated(180, 0, 0, 1);
			glTranslated(0.5, 0, 0);
			drawLeg();
		glPopMatrix();

		glPushMatrix();
			glTranslated(1, 0, 3);
			glRotated(90, 0, 1, 0);
			drawArm();
		glPopMatrix();

		glPushMatrix();
			glTranslated(-1, 0, 3);
			glRotated(270, 0, 1, 0);
			drawArm();
		glPopMatrix();

		glPushMatrix();
			glTranslated(0, 0, 3);
			drawHead();
		glPopMatrix();

	glPopMatrix();
}

void drawModel()
{
	setAmbientColor(.1f,.1f,.1f);
	setDiffuseColor(COLOR_GREEN);
	
	glPushMatrix();
		glTranslated(VAL(XPOS), VAL(YPOS), VAL(ZPOS));
		drawBody();
	glPopMatrix();
}
void drawModel2()
{
	glScalef(VAL(WIDTH),VAL(HEIGHT),2.0f);
	glTranslated(VAL(XPOS), VAL(YPOS), VAL(ZPOS));
	glTranslated(0, 0.5, 0);
	glRotated(-90, 0.0, 1.0, 0.0);
	glRotated(VAL(ROTATE), 0.0, 1.0, 0.0);
	setDiffuseColor(VAL(RED),VAL(GREEN),VAL(BLUE));
	//setDiffuseColor(COLOR_GREEN);
	//left antenna
	glPushMatrix();
		glRotated(125, 1.0, 0.0, 0.0);
		glTranslated(0.0, -0.05, -1.4);
		drawSphere(0.05);
		drawCylinder(0.6, 0.05, 0.05);
	glPopMatrix();
	//right antenna
	glPushMatrix();
		glRotated(55, 1.0, 0.0, 0.0);
		glTranslated(0.0, 0.05, -1.5);
		drawSphere(0.05);
		drawCylinder(0.6, 0.05, 0.05);
	glPopMatrix();
	//head
	drawSphere(0.9);
	//body
	glPushMatrix();
		glRotated(90, 1.0, 0.0, 0.0);
		drawCylinder(1, 0.9, 0.9);
	glPopMatrix();
	//left arm
	glPushMatrix();
		glRotated(VAL(LEFTARMYROTATION), 0.0, 1.0, 0.0);
		glTranslated(0, 0, -1.8);
		drawSphere(0.2);
		drawCylinder(1, 0.2, 0.2);
	glPopMatrix();
	//right arm
	glPushMatrix();
		glRotated(VAL(RIGHTARMYROTATION), 0.0, 1.0, 0.0);
		glTranslated(0, 0, -1.7);
		drawSphere(0.2);
		drawCylinder(1, 0.2, 0.2);
	glPopMatrix();
	//left leg
	glPushMatrix();
		glRotated(-90, 1.0, 0.0, 0.0);
		glTranslated(0, -0.5, -1.8);
		drawSphere(0.2);
		drawCylinder(0.8, 0.2, 0.2);
	glPopMatrix();
	//right leg
	glPushMatrix();
		glRotated(-90, 1.0, 0.0, 0.0);
		glTranslated(0, 0.5, -1.8);
		drawSphere(0.2);
		drawCylinder(0.8, 0.2, 0.2);
	glPopMatrix();
}
// We are going to override (is that the right word?) the draw()
// method of ModelerView to draw out SampleModel
void SampleModel::draw()
{
    // This call takes care of a lot of the nasty projection 
    // matrix stuff.  Unless you want to fudge directly with the 
	// projection matrix, don't bother with this ...
    ModelerView::draw();

	//drawFloor();

	//drawModel();
	//drawSea();
	drawModel2();
}

int main()
{
	// Initialize the controls
	// Constructor is ModelerControl(name, minimumvalue, maximumvalue, 
	// stepsize, defaultvalue)
    ModelerControl controls[NUMCONTROLS];
    controls[XPOS] = ModelerControl("X Position", -5, 5, 0.1f, 0);
    controls[YPOS] = ModelerControl("Y Position", 0, 5, 0.1f, 0);
    controls[ZPOS] = ModelerControl("Z Position", -5, 5, 0.1f, 0);
    controls[HEIGHT] = ModelerControl("Height", 1, 2.5, 0.1f, 1);
    controls[WIDTH] = ModelerControl("Width", 1, 2.5, 0.1f, 1);
	controls[LEFTARMYROTATION] = ModelerControl("Left Arm Y Rotation", -80, 80, 10, 0);
	controls[RIGHTARMYROTATION] = ModelerControl("Right Arm Y Rotation", 260, 100, 10, 180);
	controls[RED] = ModelerControl("Red", 0.0f, 1.0f, 0.1f, 0.0f);
	controls[GREEN] = ModelerControl("Green", 0.0f, 1.0f, 0.1f, 1.0f);
	controls[BLUE] = ModelerControl("Blue", 0.0f, 1.0f, 0.1f, 0.0f);
	controls[ROTATE] = ModelerControl("Rotate", 0, 360, 1, 0);

    ModelerApplication::Instance()->Init(&createSampleModel, controls, NUMCONTROLS);
    return ModelerApplication::Instance()->Run();
}

